package com.xiangmo.travel.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.CommunityCommentsDto;
import com.xiangmo.travel.Dto.PageInfoDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Dvo.CommunityCommentsDvo;
import com.xiangmo.travel.Entity.CommunityComments;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.CommunityCommentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/community/comment")
public class CommunityCommentsController {
    @Autowired
    private CommunityCommentsService communityCommentsService;
    //添加评论
    @TokenRequired
    @PostMapping("/add")
    public ResultData<CommunityComments> addComment(@RequestBody CommunityCommentsDto communityCommentsDto,
                                                      @RequestHeader("Authorization") String token){
        if(communityCommentsService.addComment(communityCommentsDto) == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("社区添加评论失败!");
        return new ResultData<>(CodeAndMsg.COMMENT_ERROR.getCode(),
                CodeAndMsg.COMMENT_ERROR.getMessage(), null, token.substring(7));
    }
    //根据论坛帖子id 查询父评论
    @TokenRequired
    @GetMapping("/page/{id}")
    public ResultData<PageInfoDto<CommunityCommentsDvo>> getCommentPageByCommunityId(@PathVariable Long id,
                                                                              @RequestParam int pageNum,
                                                                              @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageInfoDto<CommunityCommentsDvo> page = communityCommentsService.getCommentByCommunityId(id, pageNum, pageSize);
        if(page != null){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
        }
        log.warn("社区添加评论失败!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
    }

    //登录后 根据论坛帖子id 查询父评论
    @TokenRequired
    @GetMapping("/page/{id}/{userId}")
    public ResultData<PageInfoDto<CommunityCommentsDvo>> getCommentPageByCommunityIdByUserId(@PathVariable Long id,@PathVariable Long userId,
                                                                                     @RequestParam int pageNum,
                                                                                     @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageInfoDto<CommunityCommentsDvo> page = communityCommentsService.getCommentByCommunityIdAndUserId(userId, id, pageNum, pageSize);
        if(page != null){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
        }
        log.warn("根据论坛帖子id 查询父评论失败!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
    }

    //根据论坛父评论id 查询子评论
    @TokenRequired
    @GetMapping("/page/child/{id}")
    public ResultData<PageInfoDto<CommunityCommentsDvo>> getChildCommentPageByCommunityId(@PathVariable Long id,
                                                                                     @RequestParam int pageNum,
                                                                                     @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageInfoDto<CommunityCommentsDvo> page = communityCommentsService.getChildCommentByCommunityId(id, pageNum, pageSize);
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
    }

    //登录后根据论坛父评论id 查询子评论
    @TokenRequired
    @GetMapping("/page/child/{id}/{userId}")
    public ResultData<PageInfoDto<CommunityCommentsDvo>> getChildCommentPageByCommunityIdAndUserId(@PathVariable Long userId, @PathVariable Long id,
                                                                                          @RequestParam int pageNum,
                                                                                          @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        PageInfoDto<CommunityCommentsDvo> page = communityCommentsService.getChildCommentByCommunityIdAndUserId(userId, id, pageNum, pageSize);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
    }

    //删除评论
    @TokenRequired
    @DeleteMapping("/delete/{id}")
    public ResultData<Page<CommunityComments>> deleteCommentPageById(@PathVariable Long id,
                                                                       @RequestHeader("Authorization") String token){
        //判断该评论是否是用户自己的
        //执行删除操作
        int status = communityCommentsService.deleteCommentById(id);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.DELETE_SUCCESS.getCode(),
                    CodeAndMsg.DELETE_SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("论坛删除评论失败!");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
    }
    //回复评论
    @TokenRequired
    @PostMapping("/add/child")
    public ResultData<Page<CommunityComments>> addChildComment(@RequestBody CommunityCommentsDto communityCommentsDto,
                                                                 @RequestHeader("Authorization") String token){
        if(communityCommentsService.addChildComment(communityCommentsDto) == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
        }
        log.warn("论坛回复评论失败!");
        return new ResultData<>(CodeAndMsg.COMMENT_ERROR.getCode(),
                CodeAndMsg.COMMENT_ERROR.getMessage(), null, token.substring(7));
    }
}
